package E2013;

import com.sun.corba.se.impl.protocol.giopmsgheaders.FragmentMessage;

import javax.swing.*;
import java.awt.event.*;

public class e09 {

    public void createAndShow(){
        // 设置主窗体
        JFrame frame = new JFrame("文本输出");
        frame.setSize(500,300);
        frame.setLocation(500,500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        // 创建中间容器
        JPanel jPanel = new JPanel();
        JTextField textField = new JTextField(10);
        JTextArea textArea = new JTextArea(3,20);
        textField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode()==10){
                    textArea.setText(textField.getText()+"\n"+textField.getText()+"\n"+textField.getText());
                }
            }
        });

        // 添加按钮组件清空内容
        JButton jButton = new JButton("清空");
        // 为按钮添加监听事件  --- 注册监听到事件源
        jButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                textField.setText("");
                textArea.setText("");
            }
        });
        // 添加组件
        jPanel.add(textField);
        jPanel.add(textArea);
        jPanel.add(jButton);
        frame.add(jPanel);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                e09 e = new e09();
                e.createAndShow();
            }
        });
    }
}
